<?php

class MyHelper 
{
	public static function byte_format($number)
	{
		$ext = 'Byte';
		if ( $number > 1000 )
		{
			$number = $number / 1024;
			$ext = "KB";
		}
		
		if ($number > 1000)
		{
			$number = $number / 1000;
			$ext = "MB";
		}
		
		if ($number > 1000)
		{
			$number = $number / 1000;
			$ext = "GB";
		}
		
		$number = round($number,1);
		return $number." ". $ext;
	}
	
	public static function currency_format($number)
	{
		return number_format($number, 0, ',', '.');
	}
	
	public static function check_valid_email($email)
	{
		return preg_match('|^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $email);
	}
	
	public static function check_valid_date($year, $month, $day)
	{
		if ($year == 0) return false;
		if ($month == 0) return false;
		
		$num_day_of_month = self::num_day_of_month($year, $month);
		return ($day >= 1) && ($day <= $num_day_of_month);
	}
	
	public static function num_day_of_month($year = 0, $month = 0)
	{
		$month = (int)$month;
		$year = (int)$year;
		
		$day_of_month = array(1=>31, 2=>28,	3=>31, 4=>30, 5=>31, 6=>30, 
					7=>31, 8=>31, 9=>30, 10=>31, 11=>30, 12=>31);
		
		if ($year % 4 == 0 )
			$day_of_month[2] = 29;
		
		return $day_of_month[$month];
	}
	
	public static function get_year()
	{
		$datetime = getdate();
		return $datetime["year"];
	}
	
	public static function get_month()
	{
		$datetime = getdate();
		return $datetime["mon"];
		
	}
	
	public static function get_day()
	{
		$datetime = getdate();
		return $datetime["mday"];
		
	}
	public static function get_date_parse($datetime)
	{
		$dt = date_parse($datetime);
		$year = $dt['year'];
		$month = $dt['month'];
		$day = $dt['day'];
		
		return self::num2($year)."-".self::num2($month)."-".self::num2($day);
	}
	
	public static function get_datetime()
	{
		$datetime = getdate(time());
		$date = self::num2($datetime["year"])."-".
				self::num2($datetime["mon"])."-".
				self::num2($datetime["mday"]);
				
		$time = $datetime["hours"].":".
				self::num2($datetime["minutes"]).":".
				self::num2($datetime["seconds"]);
		
		return $date . " " . $time;
	}
	public static function get_date()
	{
		$datetime = getdate(time());
		return self::num2($datetime["year"])."-".
				self::num2($datetime["mon"])."-".
				self::num2($datetime["mday"]);
	}
	
	public static function get_time()
	{
		$datetime = getdate(time());
		return $datetime["hours"].":".
				self::num2($datetime["minutes"]).":".
				self::num2($datetime["seconds"]);
	}
	
	public static function num2($num)
	{
		if ( $num == 0 ) return "";
		if ($num >0 && $num < 10 )
			return "0".$num;
		else
			return $num;
	}
	
	public static function rand_string($len = 32)
	{
		$length = $len;
		$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		$string = "";    
	
		for ($p = 0; $p < $length; $p++)
		{
			$string .= $characters[mt_rand(0, strlen($characters)-1)];
		}
	
		return $string;
	}
	
	public static function convert_no_sign($str)
	{
		$str = preg_replace("/(&)/", '', $str);
		$str = preg_replace("/(\/)/", '', $str);
		
		$str = preg_replace("/(  )/", ' ', $str);
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);
		$str = preg_replace("/( )/", '-', $str);
		return $str;
	}
}